require 'View/UIBase/UIBase'
require 'View/UICost/UIMonthCard'
require 'View/UICost/UIBornGift'
require 'View/UICost/UIDailyGift'
require 'View/UICost/UIOptionalGift'
require 'View/UICost/UIGiftGrowUp'
require 'View/UICost/UIBattlePass'
require 'View/UICost/UIGiftVip'
require 'View/UICost/UIActivityPublicBeta'
require 'View/UICost/UIActivityPrivilege'
require 'View/UICost/UIGiftDay'
require 'View/UIGiftHero/UIGiftHero'

UICost = UIBase.New(PanelResNames.UICost)

function UICost.Init(func, id)
    logicMgr.CostManager.setNeedShowCsotId(id);
    UIBase.Init(UICost, func)
end

-- 创建回调
function UICost:OnCreate()
    --AddUIPackageDepend(PanelResNames.UICost,"FUI/BusinessAtlas")
    --AddUIPackageDepend(PanelResNames.UICost,"FUI/BusinessAtlas3")
    self.packages = {};
    self.fristId = 0;
    self.secondId = 0;
    self.lastActiveObj = nil;
    self.activeObj = {}
    self.ToggleContentItems = {}

    self.fristMoney = self:GetChild("fristMoney")
    self.secondMoney = self:GetChild("secondMoney")
    self.threeMoney = self:GetChild("threeMoney")
    self.bg = self:GetChild("bg")
    self.root = self:GetChild("root")
    self.showSecondList = self:GetController("showSecondList")
    self.leftshow = self:GetController("leftshow")
    self.rightshow = self:GetController("rightshow")
    self.fristTagList = self:SetDynamicList(self:GetChild("list"),self.RefreshFristTagItem, self, true)
    self.secondTagList = self:SetDynamicList(self:GetChild("list2"),self.RefreshSecondTagItem, self, true)
    self.point = self:GetChild("point")
    self.titlebg = self:GetChild("n57")
    self:addEvent();
end

-- 显示
function UICost:OnEnable()
    AudioManager.Play(20)
    self.fristId, self.secondId = logicMgr.CostManager.getFristIdAndSecondId();
    self.fristIndex = 1;
    self.secondIndex = 1;
    self:freshGameMoney(35, 1);
    self:Refresh();
    logicMgr.HeroManager.setGetNewHeroShow(true);
    logicMgr.CostManager.updateNotOpenActive();
end

function UICost:addEvent()
    self:RegisterOnClick(self:GetChild("back"),self.onBack);
    self:RegisterOnClick(self:GetChild("left"),self.onLeft);
    self:RegisterOnClick(self:GetChild("right"),self.onRight);
    self.fristTagList.onClickItem:Add(self.ClickFristTag, self);
    self.secondTagList.onClickItem:Add(self.ClickSecondTag, self);
    self:RegisterEvent(Config.EventType.Fresh_Currency,self.updateGameMoney);
    self:RegisterEvent(Config.EventType.UICostUpdate,self.Refresh);
    self:RegisterEvent(Config.EventType.Fresh_CostRedDot,self.UpdateTag);
end

function UICost:removeEvent()
    self.fristTagList.onClickItem:Clear();
    self.secondTagList.onClickItem:Clear();
end

function UICost:Refresh()
    self:UpdateTag();
    self:UpdateActive();
    self:UpdateDirection();
end

function UICost:UpdateTag()
    self.fristTagData = logicMgr.CostManager.getFristClassData();
    if #self.fristTagData <= 0 then
        return ;
    end
    if #self.fristTagData <= 30 then
        self.fristTagList.scrollPane.touchEffect = false;
    else
        self.fristTagList.scrollPane.touchEffect = true;
    end
    self.fristTagList.numItems = #self.fristTagData;
    self.secondTagData = logicMgr.CostManager.getSecondClassDataByFristIndex(self.fristIndex);
    --self.secondTagList.width = 176 * #self.secondTagData + 80;
    if #self.secondTagData <= 5 then
        self.secondTagList.scrollPane.touchEffect = false;
    else
        self.secondTagList.scrollPane.touchEffect = true;
    end
    self.secondTagList.numItems = #self.secondTagData;
end

function UICost:UpdateActive()
    self:updateListPoint(false);
    local data = logicMgr.CostManager.getCostDataByFristIndexAndSecondIndex(self.fristIndex, self.secondIndex);
    if data == nil or next(data) == nil then
        return ;
    end
    if self.lastActiveObj ~= nil then
        self.lastActiveObj.fguiObj.visible = false;
        if self.lastActiveObj.OnHide then
            self.lastActiveObj:OnHide()
        end
    end
    self:UpdateCostBg();
    self.showSecondList.selectedIndex = 1;
    local id = data.id;
    self.lastActiveId = id;
    if id == nil then
        return
    end
    local curData = logicMgr.CostManager.getCostTableDataById(id);
    if self.activeObj[id] == nil then
        local fObj = nil;
        local obj = nil;
        if curData ~= nil then
            local name, package = getUIName(PanelResNames[curData.f_Interface]);
            AddUIPackage(package);
            AddUIPackageDepend(PanelResNames.UICost,package)
            table.insert(self.packages,package);
            fObj = newGComponent(name,package);
            fObj.height = fObj.height + GRoot.inst.height - 1920;
            fObj.width = fObj.width + GRoot.inst.width - 1080 -2*Config.uiXOffset;
            self.root:AddChild(fObj);
            if panelMgr.isNeedFit then
                --fObj.y = 100
                local fit = fObj:GetController("Fit");
                if fit ~= nil then
                    fit.selectedIndex = 1;
                end
            end
            
            obj = _G[name].New(fObj, id, self);
        end
        self.activeObj[id] = obj;
    end
    if self.activeObj[id] ~= nil then
        self.activeObj[id].fguiObj.visible = true;
        if curData.f_MenuClass == 1 or #self.secondTagData <=1 then
            self.showSecondList.selectedIndex = 0;
        end
        self.activeObj[id]:Refresh();
    end
    self.lastActiveObj = self.activeObj[id];
    logicMgr.CostManager.setNeedShowCsotId(id);
end

--是否显示向下提示箭头
function UICost:updateListPoint(value)
    self.point.visible = value;
end

--游戏货币
function UICost:updateGameMoney()
    self:freshGameMoney(self.fristMoneyId, self.secondMoneyId);
end

--游戏货币
function UICost:updateCostShowByTableOrJson(data)
    if type(data) ~= "table" then
        data = decodeJsonStrToTabel(data);
    end
    self:freshGameMoney(data[2], data[1]);
end

--游戏货币
function UICost:freshGameMoney(id1, id2)
    self.fristMoneyId = id1;
    self.secondMoneyId = id2;
    if id1 == nil then
        self.fristMoney.visible = false;
    else
        self.fristMoney.visible = true;
        local item1 = logicMgr.ItemManager.getItemConstDatabyModelId(id1);
        if item1 ~= nil then
            self.fristMoney:GetChild("icon").url = EnumConst.AtlasPath.ItemAtlas..item1.icon;
        end
        if tonumber(id1) == CurrencyEnum.MONEY then
            self.fristMoney:GetChild("title").text = getNumString(logicMgr.ItemManager.getCurrencyCountByModelId(tonumber(id1)));
        else
            self.fristMoney:GetChild("title").text = (logicMgr.ItemManager.getCurrencyCountByModelId(tonumber(id1)));
        end
    end
    if id2 == nil then
        self.secondMoney.visible = false;
    else
        self.secondMoney.visible = true;
        local item2 = logicMgr.ItemManager.getItemConstDatabyModelId(id2);
        if item2 ~= nil then
            self.secondMoney:GetChild("icon").url = EnumConst.AtlasPath.ItemAtlas..item2.icon;
        end
        if tonumber(id2) == CurrencyEnum.MONEY then
            self.secondMoney:GetChild("title").text = getNumString(logicMgr.ItemManager.getCurrencyCountByModelId(tonumber(id2)));
        else
            self.secondMoney:GetChild("title").text = (logicMgr.ItemManager.getCurrencyCountByModelId(tonumber(id2)));
        end
    end

    local cashCouponNum = logicMgr.ItemManager.getBagNumByTableID(200028);
    if cashCouponNum > 0 then
        self.threeMoney.visible = true;
        self.threeMoney:GetChild("title").text = cashCouponNum;
        local item3 = logicMgr.ItemManager.getItemConstDatabyModelId(200028);
        if item3 ~= nil then
            self.threeMoney:GetChild("icon").url = EnumConst.AtlasPath.ItemAtlas..item3.icon;
        end
    else
        self.threeMoney.visible = false;
    end

end

function UICost:RefreshFristTagItem(index, itemObj)
    local id = self.fristTagData[index].fristId;
    local curData = logicMgr.CostManager.getCostTagTableDataById(id);
    if curData ~= nil and next(curData) ~= nil then
        --local upIcon = itemObj:GetChild("icon");
        local downIcon = itemObj:GetChild("bg");
        local title = itemObj:GetChild("title");
        local redDot = itemObj:GetChild("UIRedDots");
        downIcon.url = EnumConst.AtlasPath.ICONAtlas..curData.f_IconClosePic;
        --downIcon.url = EnumConst.AtlasPath.ICONAtlas..curData.f_IconOpenPic;
        title.text = getLanguage(curData.f_IconName, nil , curData.f_IconName);
        redDot.visible = logicMgr.CostManager.getTagRedDotById(id);
        --itemObj.onClick:Clear();
        --itemObj.onClick:Add(function() self:ClickFristTag(index); end);
    else
        itemObj.visible = false;
    end
    if self.fristId == id then
        self.fristIndex = index;
        itemObj.selected = true;
        self.fristTagList:ScrollToView(index - 1);
    else
        itemObj.selected = false;
    end
end

function UICost:RefreshSecondTagItem(index, itemObj)
    local id = self.secondTagData[index].secondId;

    local curData = logicMgr.CostManager.getCostTagTableDataById(id);
    if curData ~= nil and next(curData) ~= nil then
        --local upIcon = itemObj:GetChild("icon");
        local downIcon = itemObj:GetChild("bg");
        local title = itemObj:GetChild("title");
        local redDot = itemObj:GetChild("UIRedDots");
        local mask = itemObj:GetChild("mask");
        downIcon.url = EnumConst.AtlasPath.ICONAtlas..curData.f_IconClosePic;
        mask.url = EnumConst.AtlasPath.ICONAtlas..curData.f_IconClosePicMask;
        --downIcon.url = EnumConst.AtlasPath.ICONAtlas..curData.f_IconOpenPic ;
        title.text = getLanguage(curData.f_IconName, nil , curData.f_IconName);
        redDot.visible = logicMgr.CostManager.getTagRedDotById(id);
        itemObj.visible = true;
        --itemObj.onClick:Clear();
        --itemObj.onClick:Add(function() self:ClickSecondTag(index); end);
    else
        itemObj.visible = false;
    end

    if self.secondId == id then
        self.secondIndex = index;
        itemObj.selected = true;
		self.secondTagList:ScrollToView(index - 1);
    else
        itemObj.selected = false;
    end
end

function UICost:ClickFristTag(context)
    local go = context.data
    local index = self.fristTagList:GetChildIndex(go);
    self.fristIndex = index + 1;
    self.secondIndex = 1;
    self.fristId, self.secondId, self.activeId = logicMgr.CostManager.getFristIdAndSecondIdByFristIndexAndSecondIndex(self.fristIndex, self.secondIndex);
    if self.lastActiveId ~= self.activeId then
        self:Refresh();
    end
end

function UICost:ClickSecondTag(context)
    local go = context.data
    local index = self.secondTagList:GetChildIndex(go);
    self.secondIndex = index + 1;
    self.fristId, self.secondId, self.activeId = logicMgr.CostManager.getFristIdAndSecondIdByFristIndexAndSecondIndex(self.fristIndex, self.secondIndex, true);
    if self.lastActiveId ~= self.activeId then
        self:UpdateActive();
    end
end

function UICost:UpdateDirection()
    if not self or self.fristIndex == nil then
        return
    end
    if self.fristIndex <= 1 then
        self.leftshow.selectedIndex = 0;
    else
        self.leftshow.selectedIndex = 1;
    end
    if self.fristIndex >= #self.fristTagData then
        self.rightshow.selectedIndex = 0;
    else
        self.rightshow.selectedIndex = 1;
    end
end

function UICost:onLeft()
    if self.fristIndex <= 1 then
        self:UpdateDirection();
        return;
    end
    self.fristIndex = self.fristIndex - 1;
    self.secondIndex = 1;
    self.fristId, self.secondId, self.activeId = logicMgr.CostManager.getFristIdAndSecondIdByFristIndexAndSecondIndex(self.fristIndex, self.secondIndex);
    self:Refresh();
end

function UICost:onRight()
    if self.fristIndex >= #self.fristTagData then
        self:UpdateDirection();
        return;
    end
    self.fristIndex = self.fristIndex + 1;
    self.secondIndex = 1;
    self.fristId, self.secondId, self.activeId = logicMgr.CostManager.getFristIdAndSecondIdByFristIndexAndSecondIndex(self.fristIndex, self.secondIndex);
    self:Refresh();
end

function UICost:UpdateCostBg(bgUrl, color)
    if bgUrl == nil then
        bgUrl = "";
    end
    self.bg.url = bgUrl;
    self.bg.color = color or Color.New(1, 1, 1);
end

-- 隐藏
function UICost:onBack()
    self:OnBack();
    logicMgr.CostManager.clearId();
    logicMgr.HeroManager.setGetNewHeroShow();
    AudioManager.RefreshBGM()
end

-- 隐藏
function UICost:OnDisable()
end

-- 卸载
function UICost:OnDestroy()
    self:removeEvent();
    for k, v in pairs(self.activeObj) do
        v:Destroy();
    end
    for i,package in pairs(self.packages) do
        if UIPackage.GetById(package) then
            UIPackage.RemovePackage(package)
        end
    end
end

return UICost